'use strict';

import * as moment from 'moment'

class PlayerBakDao {
    handle: any
    constructor(db) {
        this.handle = db.collection("playerBak")
        this.handle.createIndex("pID", -1)
    }

    newAndSave = async (pID, playerData): Promise<any> => {
        let date = new Date().getTime()
        return await this.handle.insertOne({
            pID: pID,
            time1: date,
            time2: date,
            playerData1: playerData,
            playerData2: playerData
        });
    }

    findOne = async (query, projection?:any): Promise<any> => {
        return await this.handle.findOne(query, projection)
    }

    find = async (query): Promise<any[]> => {
        return await this.handle.find(query).toArray()
    }

    findByPage = async (query, start: number, size: number): Promise<any[]> => {
        return await this.handle.find(query).skip(start).limit(size).toArray()
    }

    findByPageAndSort = async (query, sort, start, size): Promise<any[]> => {
        return await this.handle.find(query).sort(sort).skip(start).limit(size).toArray()
    }

    findAndSort = async (query, sort): Promise<any[]> => {
        return await this.handle.find(query).sort(sort).toArray()
    }
    //opts  { multi: true}
    updateMany = async (query, data): Promise<any> => {
        return await this.handle.updateMany(query, data);
    }

    updateOne = async (query, data, opts?: any): Promise<any> => {
        return await this.handle.updateOne(query, data, opts || {});
    }

    count = async (query): Promise<number> => {
        return await this.handle.countDocuments(query)
    }

    remove = async (query): Promise<any> => {
        return await this.handle.remove(query, {
            multi: true
        });
    }
}

export default PlayerBakDao